package net.sureon.web.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.AppContext;
import net.sureon.biz.CompanyMgr;
import net.sureon.biz.UserBO;
import net.sureon.common.exception.SureonException;
import net.sureon.common.security.Action;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class ConsumeRecord implements Controller {

	private CompanyMgr companyMgr;
	
	/**
	 * @return the companyMgr
	 */
	public CompanyMgr getCompanyMgr() {
		return companyMgr;
	}

	/**
	 * @param companyMgr the companyMgr to set
	 */
	public void setCompanyMgr(CompanyMgr companyMgr) {
		this.companyMgr = companyMgr;
	}

	private static final String VIEW_CONSUME_RECORD = "admin/consumeRecord";
	private static final String PARAM_VIEW = "view";
	private static final String PARAM_VALUE_BALANCE = "balance";
	private static final String PARAM_ATTR_NAME_RECORD = "records";

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		List records;
		if(PARAM_VALUE_BALANCE.equals(request.getParameter(PARAM_VIEW))){
			result.put(PARAM_VIEW, PARAM_VALUE_BALANCE);
			records = balanceRecord();
			
		}else{
			records = consumeRecord();
		}
		result.put(PARAM_ATTR_NAME_RECORD, records);
		return new ModelAndView(VIEW_CONSUME_RECORD, result);
	}

	private List consumeRecord() throws SureonException {
		return companyMgr.getRecentTenConsumeRecord(AppContext.getCompanyID());
	}
	
	private List balanceRecord() throws SureonException {
		return companyMgr.getRecentTenBalanceOperation(AppContext.getCompanyID());
	}
}
